//登录参数实体类

import {ref,reactive,getCurrentInstance,watch,defineEmits} from 'vue'
import Pattern from '@/utils/pattern'
import type { FormInstance,FormRules,Action } from 'element-plus'

interface LoginFormClass{
  mobile:string,
  password:string,
  imgCode:string,
  imgCodeToken:string
}

interface RegFormClass{
  mobile:string,
  nickName:string
}

interface ForgetFormClass{
  mobile:string,
  shortMessageCode:string,
  passwordOriginal:string,
  passwordConfirm:string
}

interface SendSMFormClass{
  mobile:string
}

export default function useForm(t:any) {
  const {proxy}:any = getCurrentInstance()

  //dialog变量
  const titleId=ref("")
  const titleClass=ref("titleClass")

  //登录用户缓存
  const localUser={}

  //表单变量
  const loginForm=reactive<LoginFormClass>({
    mobile:'',
    password:'',
    imgCode:'',
    imgCodeToken:''
  })

  const regForm:RegFormClass=reactive({
    mobile:'',
    nickName:''
  })

  const forgetForm=reactive<ForgetFormClass>({
    mobile:'',
    shortMessageCode:'',
    passwordOriginal:'',
    passwordConfirm:''
  })

  const sendSMForm=reactive<SendSMFormClass>({
    mobile:''
  })

  //表单别名
  const loginFormRef=ref<FormInstance>()
  const regFormRef=ref<FormInstance>()
  const forgetFormRef=ref<FormInstance>()
  const sendSMFormRef=ref<FormInstance>()

  //表单校验
  const mobileValidator:any=(rule:any,value:any,callback:any)=>{
    if(!value || value==''){
      callback(new Error(t('error.emptyPhone')))
    }else if(value!='' && value.length>11){
      callback(new Error(t('error.errorPhone')))
    }else{
      if(!Pattern.validatePhone(value)){
        callback(new Error(t('error.regPhone')))
      }else{
        callback()
      }
    }
  }
  
  const passwordValidator=(rule:any,value:any,callback:any)=>{
    if(!value || value==''){
      callback(new Error(t('error.emptyPwd')))
    }else if(!Pattern.validatePasswordWeak(value)){
      callback(new Error(t('error.errorPwd')))
    }else{
      callback()
    }
  }
  
  const passwordConfValidator=(rule:any,value:any,callback:any)=>{
    console.log("forgetForm.passwordOriginal: ",forgetForm)
    console.log("value-----",value)
    if(!value || value==''){
      callback(new Error(t('error.emptyPwd')))
    }else if(!Pattern.validatePasswordWeak(value)){
      callback(new Error(t('error.errorPwd')))
    }else if(value!=forgetForm.passwordOriginal){
      callback(new Error(t('error.confPwd')))
    }else{
      callback()
    }
  }
  
  const imgCodeValidator=(rule:any,value:any,callback:any)=>{
    if(!value || value==''){
      callback(new Error(t('error.emptyCaptchaCode')))
    }else if(value.length<4){
      callback(new Error(t('error.errorCaptchaCode')))
    }else{
      callback()
    }
  }
  
  const shortMessageValidator=(rule:any,value:any,callback:any)=>{
    if(!value || value==''){
      callback(new Error(t('error.emptySms')))
    }else if(value.length<6){
      callback(new Error(t('error.errorSms')))
    }else{
      callback()
    }
  }

  //element校验rule
  const loginRules=reactive<FormRules<typeof loginForm>>({
    mobile:[{validator:mobileValidator,trigger:'blur'}],
    password:[{validator:passwordValidator,trigger:'blur'}],
    imgCode:[{validator:imgCodeValidator,trigger:'blur'}]
  })

  const regRules=reactive<FormRules<typeof regForm>>({
    mobile:[{validator:mobileValidator,trigger:'blur'}]
  })

  const forgetRules=reactive<FormRules<typeof forgetForm>>({
    mobile:[{validator:mobileValidator,trigger:'blur'}],
    shortMessageCode:[{validator:shortMessageValidator,trigger:'blur'}],
    passwordOriginal:[{validator:passwordValidator,trigger:'blur'}],
    passwordConfirm:[{validator:passwordConfValidator,trigger:'blur'}]
  })

  const sendSMRules=reactive<FormRules<typeof sendSMForm>>({
    mobile:[{validator:mobileValidator,trigger:'blur'}]
  })


  return {
    titleId,
    titleClass,
    localUser,
    loginForm,
    loginFormRef,
    loginRules,
    regForm,
    regFormRef,
    regRules,
    forgetForm,
    forgetFormRef,
    sendSMForm,
    sendSMFormRef,
    forgetRules,
    sendSMRules
  }
}